package com.ucar.train.web;

import com.ucar.train.dto.CarDTO;
import com.ucar.train.dto.StoreCarMappingDTO;
import com.ucar.train.dto.StoreDTO;
import com.ucar.train.service.impl.CarServiceImp;
import com.ucar.train.service.impl.StoreServiceImpl;
import com.ucar.train.utils.GsonUtils;
import com.ucar.train.utils.JodaUtils;
import com.ucar.train.vo.CarVO;
import com.ucar.train.vo.Store;
import com.ucar.train.vo.StoreVO;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import java.util.ArrayList;
import java.util.List;

/**
 * @author zjm
 * @date 2019/8/13
 */
@Controller
@RequestMapping("/store")
@Slf4j
public class StoreController {

    @Autowired
    private StoreServiceImpl storeService;

    @Autowired
    private CarServiceImp carServiceImp;


    @RequestMapping(value = "/car",method = RequestMethod.POST)
    @ResponseBody
    public String getCarByStoreCode(String storeCode) {
        List<StoreCarMappingDTO> storeCarMappingDTOS = storeService.selectCarInfoByStoreId(storeCode);
//        System.out.println(storeCarMappingDTOS.get(1).getCarDTO().getType());
        List<CarVO> carVOS = new ArrayList<>();
        CarVO carVO = new CarVO();
        for(StoreCarMappingDTO storeCarMappingDTO: storeCarMappingDTOS) {
            carVO.setCarId(storeCarMappingDTO.getCarId());
            carVO.setType(storeCarMappingDTO.getCarDTO().getType());
            carVO.setCost(storeCarMappingDTO.getCost());
            carVOS.add(carVO);
        }
//        System.out.println(carVOS.get(0).getCarId());
        return GsonUtils.toJson(carVOS);
    }

    /**
     * @Description
     * @author Helingang
     * @param storeId
     * @createDate 2019/8/15
     **/
    @RequestMapping(value = "/showStoreAndCar",method = RequestMethod.GET)
    public ModelAndView selectStorebyStoreId( @RequestParam(name = "storeId", required = true, defaultValue = "A001") String storeId) {
        List<StoreDTO> storeDTOS = storeService.selectAllStore();
        List<StoreVO> storeVOS=new ArrayList<>(storeDTOS.size());
        for (StoreDTO storeDTO : storeDTOS) {
            StoreVO storeVo=new StoreVO();
            storeVo.setId(storeDTO.getId());
            storeVo.setStoreCode(storeDTO.getStoreCode());
            storeVo.setName(storeDTO.getName());
            storeVOS.add(storeVo);
        }


        StoreDTO storeDTO = storeService.selectStorebyStoreId(storeId);
        List<CarDTO> carDTOS = carServiceImp.selectCarByStoreId(storeId);
        // DTO transfer VO
        StoreVO storeVO=new StoreVO();
        storeVO.setName(storeDTO.getName());
        List<CarVO> CarVoList=new ArrayList<> (carDTOS.size());
        for (CarDTO dto : carDTOS) {
            CarVO carVO=new CarVO();
            carVO.setStoreId(storeDTO.getStoreCode());
            carVO.setCarId(dto.getCarId());
            carVO.setCarNum(dto.getCarNum());
            carVO.setType(dto.getType());
            carVO.setState(dto.getState());
            CarVoList.add(carVO);
        }
        storeVO.setList(CarVoList);
        //ModelAndView 添加VO
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("stores", storeVO);
        modelAndView.addObject("storeVOS", storeVOS);
        modelAndView.setViewName("storeandcar-list");
        //返回ModelAndView
        log.info(storeVO.getList().size()+"");
        return modelAndView;
    }

    /**
     * @Description
     * @author Helingang
     * @param storeId
     * @createDate 2019/8/15
     **/
    @RequestMapping(value = "/schedule",method = RequestMethod.GET)
    public ModelAndView schedule(String storeId,String carId)
    {
        // 根据门店id查出门店，车编号，车牌，型号，状态
        List<CarDTO> carList = carServiceImp.selectCarByStoreId(storeId);
        StoreDTO storeDTO = storeService.selectStorebyStoreId(storeId);
        // CarVO carVO=new CarVO();
        CarDTO carDto=new CarDTO();
        for (CarDTO carDTO : carList) {
            if (carDTO.getCarId().equals(carId)){
                carDto=carDTO;
                break;
            }
        }
        CarVO carVO = CarDTOConvertToCarVO(storeDTO, carDto);
        //ModelAndView 添加VO
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("scheduleCar",carVO);
        List<StoreDTO> storeDTOS = storeService.selectAllStore();
        List<StoreVO> storeVOS=new ArrayList<>(storeDTOS.size());
        for (StoreDTO store : storeDTOS) {
            StoreVO storeVo=new StoreVO();
            storeVo.setId(store.getId());
            storeVo.setStoreCode(store.getStoreCode());
            storeVo.setName(store.getName());
            storeVOS.add(storeVo);
        }
        modelAndView.addObject("storeVOS", storeVOS);
        modelAndView.setViewName("schedule-show");
        //返回ModelAndView
        return modelAndView;

    }

    private CarVO CarDTOConvertToCarVO(StoreDTO storeDTO, CarDTO carDto) {
        CarVO carVO=new CarVO();
        carVO.setStoreId(storeDTO.getStoreCode());
        carVO.setStoreName(storeDTO.getName());
        carVO.setCarId(carDto.getCarId());
        carVO.setCarNum(carDto.getCarNum());
        carVO.setType(carDto.getType());
        carVO.setMaintenanceTime(carDto.getMaintenanceTime());
        carVO.setState(carDto.getState());
        return carVO;
    }

    /**
     * @Description  调度
     * @author Helingang
     * @param
     * @createDate 2019/8/16
     **/
    @RequestMapping(value = "/updateCar",method = RequestMethod.POST)
    public String updateCar(CarVO carVO, RedirectAttributes redirectAttributes)
    {
        log.info(carVO.toString());
        int resultRow = carServiceImp.updateByCarId(carVO);
        int updateRow = carServiceImp.updateScheduleCar(carVO.getCarId(), carVO.getStoreId());
        redirectAttributes.addAttribute("storeId", carVO.getStoreId());
        return "redirect:showStoreAndCar";
    }



    /**
     * @param storeVO
     * @Description 添加门店
     * @author LiuHao
     * @createDate 2019/8/15
     **/
    @RequestMapping(value = "/addStore", method = RequestMethod.POST)
    public ModelAndView addStore(StoreVO storeVO) {

        StoreDTO storeDTO = new StoreDTO();
        storeDTO.setName(storeVO.getName());
        storeDTO.setPosition(storeVO.getPosition());
        storeDTO.setStoreCode(storeVO.getStoreCode());
        storeDTO.setCreateTime(JodaUtils.nowAsDate());
        storeDTO.setModifyTime(JodaUtils.nowAsDate());
        return storeService.addStore(storeDTO).getData();
    }


    /**
     * @param carVO,storeCode
     * @Description 添加车辆
     * @author LiuHao
     * @createDate 2019/8/15
     **/
    @RequestMapping(value = "/addCar", method = RequestMethod.POST)
    public ModelAndView addCar(CarVO carVO) {
        CarDTO carDTO = new CarDTO();
        carDTO.setCarId(carVO.getCarId());
        carDTO.setCarNum(carVO.getCarNum());
        carDTO.setType(carVO.getType());
        carDTO.setState(carVO.getState());
        carDTO.setMaintenanceTime(carVO.getMaintenanceTime());
        carDTO.setCreateTime(JodaUtils.nowAsDate());
        carDTO.setModifyTime(JodaUtils.nowAsDate());

        //此处storeName就是storeCode
        return storeService.addCar(carDTO,carVO.getStoreName(),carVO.getCost()).getData();
    }

    /**
     *
     * @Description 返回添加门店页面
     * @author LiuHao
     * @createDate 2019/8/15
     **/
    @RequestMapping(value = "/store", method = RequestMethod.GET)
    public String store() {
        return "store-add";
    }

    /**
     *
     * @Description 返回添加车辆页面
     * @author LiuHao
     * @createDate 2019/8/15
     **/
    @RequestMapping(value = "/storeCar", method = RequestMethod.GET)
    public String storeCar() {
        return "car-add";
    }

    /**
     * @Author zhoujinmu
     * @Description ajax获取根据门店和车辆类型获取价格
     * @Date 2019/8/19 13:31
     */
    @RequestMapping(value = "/car",method = RequestMethod.GET)
    @ResponseBody
    public String getCarCostByCarTypeAndStoreCode(@RequestParam("storeCode")String storeCode,@RequestParam("carType")String carType) {
        CarVO carVO = new CarVO();
        System.out.println("test1"+storeCode);
        System.out.println(carType);
        carVO.setCost(storeService.getCarCostByCarTypeAndStoreCode(storeCode,carType));
        System.out.println(carVO);
        return GsonUtils.toJson(carVO);
    }
}
